1 package edu.jiangxin.apktoolbox.swing.extend;
2
3 import java.awt.*;
4 import java.awt.event.WindowAdapter;
5 import java.awt.event.WindowEvent;
6 import java.util.ResourceBundle;
7
8 import javax.swing.JFrame;
9
10 import org.apache.commons.configuration2.Configuration;
11 import org.apache.logging.log4j.LogManager;
12 import org.apache.logging.log4j.Logger;
13
14 import edu.jiangxin.apktoolbox.utils.Utils;
15
16
17
18
19
20
21 public class EasyFrame extends JFrame {
22 private static final long serialVersionUID = 1L;
23 protected Logger logger;
24 protected Configuration conf;
25 protected ResourceBundle bundle;
26 protected Image image;
27
28 public EasyFrame() throws HeadlessException {
29 super();
30 logger = LogManager.getLogger(this.getClass().getSimpleName());
31 conf = Utils.getConfiguration();
32 bundle = ResourceBundle.getBundle("apktoolbox");
33 addWindowListener(new WindowAdapter() {
34 @Override
35 public void windowClosing(WindowEvent e) {
36 super.windowClosing(e);
37 Utils.saveConfiguration();
38 logger.info("windowClosing: " + EasyFrame.this.getClass().getSimpleName());
39 }
40 @Override
41 public void windowIconified(WindowEvent e) {
42 super.windowIconified(e);
43 setVisible(false);
44 dispose();
45 Utils.saveConfiguration();
46 logger.info("windowIconified: " + EasyFrame.this.getClass().getSimpleName());
47 }
48 });
49 Toolkit tk = Toolkit.getDefaultToolkit();
50 image = tk.createImage(this.getClass().getResource("/icon.jpg"));
51 setIconImage(image);
52 logger.info("Frame start: " + this.getClass().getSimpleName());
53 }
54
55 public void refreshSizeAndLocation() {
56
57 pack();
58 setMinimumSize(new Dimension(800, 100));
59 setResizable(false);
60
61
62 int windowWidth = getWidth();
63 int windowHeight = getHeight();
64 Toolkit kit = Toolkit.getDefaultToolkit();
65 if (kit == null) {
66 logger.error("kit is null");
67 return;
68 }
69 Dimension screenSize = kit.getScreenSize();
70 if (screenSize == null) {
71 logger.error("screenSize is null");
72 return;
73 }
74 int screenWidth = screenSize.width;
75 int screenHeight = screenSize.height;
76 setLocation(screenWidth / 2 - windowWidth / 2, screenHeight / 2 - windowHeight / 2);
77 }
78 }